package com.probiz.estoresa.system.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.probiz.estore.common.model.system.PaymentGateway;
import com.probiz.estore.common.model.system.PaymentMethod;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.system.service.PaymentGatewayManager;
import com.probiz.estore.system.service.PaymentMethodManager;

public class PaymentMethodController extends GenericStoreAdminController<PaymentMethod> {
	private PaymentMethodManager paymentMethodManager = null;
	private PaymentGatewayManager paymentGatewayManager = null;
	private static final String PAYMENTMETHOD_LIST = "paymentMethodList";
	private static final String PAYMENTGATEWAY_LIST = "paymentGatewayList";

	public void setPaymentGatewayManager(PaymentGatewayManager paymentGatewayManager) {
		this.paymentGatewayManager = paymentGatewayManager;
	}

	public void setPaymentMethodManager(PaymentMethodManager paymentMethodManager) {
		this.paymentMethodManager = paymentMethodManager;
	}

	public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView(this.listView);
		List<PaymentMethod> paymentMethodList = paymentMethodManager.findUnDeletedPaymentMethods();
		List<PaymentGateway> paymentGatewayList = paymentGatewayManager.getAllUnAdded();
		mv.addObject(PAYMENTMETHOD_LIST, paymentMethodList);
		mv.addObject(PAYMENTGATEWAY_LIST, paymentGatewayList);
		return mv;

	}

	/**
	 * 娣诲姞涓�釜鏂扮殑鏀粯缃戝叧
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		Integer paymentGatewayId = new Integer(request.getParameter("paymentGatewayId"));
		PaymentMethod paymentMethod = paymentMethodManager.addPaymentMethod(paymentGatewayId);
		String message = getMessage("paymentMethod.added", new Object[] { paymentMethod.getPaymentMethodName() });
		saveMessage(request, message);
		return new ModelAndView(new RedirectView("paymentMethod.html"));
	}

	/**
	 * 鍒犻櫎涓�釜鏀粯缃戝叧
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) {
		Integer paymentMethodId = new Integer(request.getParameter("paymentMethodId"));
		PaymentMethod paymentMethod = paymentMethodManager.getById(paymentMethodId);
		paymentMethodManager.removePaymentMethod(paymentMethod);
		String message = getMessage("paymentMethod.deleted", new Object[] { paymentMethod.getPaymentMethodName() });
		saveMessage(request, message);
		return new ModelAndView(new RedirectView("paymentMethod.html"));
	}

	/**
	 * 閰嶇疆鏀粯缃戝叧鍙傛暟
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView config(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		Integer paymentGatewayId = new Integer(request.getParameter("paymentGatewayId"));
		PaymentGateway paymentGateway = paymentGatewayManager.getById(paymentGatewayId);
		return new ModelAndView("system/" + paymentGateway.getConfigFile(), "paymentGateway", paymentGateway);
	}
	/**
	 * 淇濆瓨鏀粯缃戝叧鍙傛暟
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView saveConfigAction(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		Integer paymentGatewayId = new Integer(request.getParameter("paymentGatewayId"));
		PaymentGateway paymentGateway = paymentGatewayManager.getById(paymentGatewayId);

		HashMap hashMap = new HashMap();
		Map paramMap = request.getParameterMap();
		Iterator it = paramMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry en = (Map.Entry) it.next();
			String[] value = (String[]) en.getValue();
			if(null != value[0]){
				hashMap.put(en.getKey(), value[0].trim());
			}else{
				hashMap.put(en.getKey(), null);
			}
		}
		paymentGateway.setConfigData(hashMap);
		paymentGatewayManager.save(paymentGateway);
		PaymentMethod paymentMethod = paymentMethodManager.getByPaymentGatewayId(paymentGatewayId);
		String message = getMessage("paymentMethod.updated", new Object[] { paymentMethod.getPaymentMethodName() });
		saveMessage(request, message);
		return new ModelAndView(new RedirectView("paymentMethod.html"));
	}

	/**
	 * 鎵逛繚瀛�
	 * @param request
	 * @param response
	 */
	public ModelAndView multiSave(HttpServletRequest request, HttpServletResponse response) {
		String[] paymentMethodIds = request.getParameterValues("paymentMethodId");
		String[] statuses = request.getParameterValues("ck_status");
		String[] paymentMethodNames = request.getParameterValues("paymentMethodName");
		String[] paymentMethodDetails = request.getParameterValues("paymentMethodDetail");
		String[] protocols = request.getParameterValues("protocol");
		String[] sortOrders = request.getParameterValues("sortOrder");
		String[] helpLinks = request.getParameterValues("helpLink");

		List<PaymentMethod> paymentMethodList = new ArrayList<PaymentMethod>();
		if (paymentMethodIds.length > 0) {
			for (int i = 0; i < paymentMethodIds.length; i++) {
				PaymentMethod paymentMethod = paymentMethodManager.getById(new Integer(paymentMethodIds[i]));
				// set payment new value;
				paymentMethod.setSortOrder(new Integer(sortOrders[i]));
				paymentMethod.setProtocol(protocols[i]);
				paymentMethod.setStatus(new Short(statuses[i]));
				paymentMethod.setPaymentMethodName(paymentMethodNames[i]);
				paymentMethod.setPaymentMethodDetail(paymentMethodDetails[i]);
				paymentMethod.setHelpLink(helpLinks[i]);
				paymentMethodList.add(paymentMethod);
			}
			// update batch
			paymentMethodManager.updateBatchPaymentMethods(paymentMethodList);
			String message = getMessage("common.updated.multi");
			saveMessage(request, message);
		}
		// save message
		return new ModelAndView(new RedirectView("paymentMethod.html"));
	}


	@Override
	protected String getEntityName(PaymentMethod entity) {
		return entity.getPaymentMethodName();
	}


	@Override
	protected void initController() throws Exception {
		this.mgr = paymentMethodManager;

	}

	@Override
	protected void preSave(HttpServletRequest request, PaymentMethod entity, BindException errors) {

	}

}
